import os
from time import sleep
from base.base import Base
import page


class PageAppSniffing(Base):

    # 点击首页-非浏览页
    def __page_app_click_no_browser_index(self):
        sleep(1)
        self.base_click(page.app_sniffing_no_browser_index)

    # 地址栏输入链接
    def __page_app_address_input(self, url):
        sleep(1)
        # 点击地址栏
        self.base_click(page.app_sniffing_address_edit)
        self.base_input(page.app_sniffing_address_edit, url)

    # 点击键盘的Enter键
    def __page_app_address_enter(self, width, height):
        sleep(1)
        # 调起搜狗输入法
        os.system("adb shell ime set com.sohu.inputmethod.sogou/.SogouIME")
        # 避免失焦
        self.base_click(page.app_sniffing_address_edit)
        sleep(1)
        self.base_tap(width, height)
        sleep(1)
        # 最后调起appiu的输入法
        os.system("adb shell ime set io.appium.settings/.UnicodeIME")

    # 手动点击播放
    def __page_app_click_player(self):
        pass

    # 点击右侧更多菜单按钮
    def __page_app_click_more_menu(self):
        sleep(1)
        self.base_click(page.app_sniffing_more_menu_btn)

    # 刷新浏览器页面
    def __page_app_web_reload(self):
        sleep(1)
        self.base_click(page.app_sniffing_reload_btn)

    # 点击嗅探按钮
    def __page_app_click_sniff_btn(self):
        sleep(1)
        self.base_click(page.app_sniffing_sniff_btn)

    # 点击下载按钮
    def page_app_click_download_btn(self):
        sleep(1)
        self.base_click(page.app_sniffing_download_btn)

    # 组合业务方法-输入地址并访问站点
    def page_sniffing(self, url, width, height):
        # 点击首页
        # self.__page_app_click_index()
        # 地址栏输入链接
        self.__page_app_address_input(url)
        # 点击键盘的Enter键
        self.__page_app_address_enter(width, height)

    # 判断嗅探是否失败
    def page_check_sniff_result(self):
        # 点击嗅探按钮
        self.__page_app_click_sniff_btn()
        sleep(1)
        flag = True
        try:
            el = self.base_find(page.app_sniffing_sniff_fail_content)
            if el:
                flag = False
        except Exception as e:
            return False
        finally:
            return flag

    # 手动根据位置点击
    def page_click_position(self, x, y):
        sleep(1)
        self.base_tap(x, y)

    # 浏览页，点击地址栏
    def page_click_address_browser_url(self):
        sleep(1)
        self.base_click(page.app_sniffing_address_browser_url)

    # 浏览页，点击回到首页
    def page_app_click_browser_index(self):
        sleep(1)
        self.base_click(page.app_sniffing_browser_index)

    # 手势下滑-通过坐标
    def page_app_swipe_position(self, x1, y1, x2, y2, duration=1000):
        sleep(1)
        self.base_swipe(x1, y1, x2, y2, duration)
